﻿//using ioman;
    using System;
    using System.Collections;
    using System.Drawing;
    using System.Windows.Forms;

namespace SheetLib
{

    public class TextControl : BaseControl
    {
        private int a;
        private int b;
        public bool bmpcreate;
        public bool bold;
        private int c;
        private Point cur;
        //public string currentLang;
        private Graphics g;
        private int Hor;
        public bool isclicked;
        //private Hashtable langText;
        private int LC;
        public string link;
        public int[] sel;
        private bool[] selected;
        private int[] sl;
        private string text;
        public int tw;
        private int Vert;
        public bool visi;
        public int watch;
        private Point[] wor;
        private string[] words;
        public int X;
        public int Y;

        public TextControl()
        {
            this.b = 1;
            //this.langText = new Hashtable();
            //this.currentLang = LangSelection.CurLang3Letter;
            this.text = "";
            this.bold = false;
            this.link = "";
            this.Init();
        }

        public TextControl(TextControl value) : base(value)
        {
            this.b = 1;
            //this.langText = new Hashtable();
            //for (int i = LangSelection.Languages.GetLength(0) - 1; i >= 0; i--)
            //{
            //    if (value.langText.ContainsKey(LangSelection.Languages[i, 0]))
            //    {
            //        this.langText.Add(LangSelection.Languages[i, 0], value.langText[LangSelection.Languages[i, 0]]);
            //    }
            //}
            //this.currentLang = value.currentLang;
            this.text = value.text;
            this.bold = value.bold;
            this.link = value.link;
            this.Init();
        }

        public override void Action(string value)
        {
            if (value != "")
            {
                this.InsertManager(value);
                if (!this.InDate)
                {
                    this.UndoRedoManager();
                }
            }
            this.PrePainter();
            this.RePainter();
        }

        public override CursorType GetCursorType(MouseEventArgs e)
        {
            return CursorType.Text;
        }

        public string GetSelectedItems()
        {
            if ((this.sel[0] == -1) && (this.sel[1] == -1))
            {
                return null;
            }
            return this.text.Substring(this.sel[0], this.sel[1] - this.sel[0]);
        }

        //public string GetText(string lang)
        //{
        //    if (this.langText.ContainsKey(lang))
        //    {
        //        return this.langText[lang].ToString();
        //    }
        //    return string.Empty;
        //}

        private void Init()
        {
            this.cur = Point.Empty;
            this.isclicked = false;
            this.bmpcreate = false;
            this.visi = true;
            this.watch = 0;
            this.sel = new int[] { -1, -1 };
            this.sl = new int[] { -1, -1 };
            this.g = Graphics.FromImage(new Bitmap(1, 1));
            this.Vert = (int) this.g.MeasureString("Х", this.Font).Height;
            this.Hor = (int) this.g.MeasureString(" ", this.Font).Width;
        }

        public void InsertManager(string value)
        {
            value = this.StringModifier(value);
            if (((value == "DELETE") || (value == "\b")) || ((value == "BACK") || (value == "CUT")))
            {
                if (((this.sl[0] != -1) && (this.sl[1] != -1)) && (this.sl[0] != this.sl[1]))
                {
                    if ((this.Text.Length > 0) && (this.watch > 0))
                    {
                        int count = (this.Text.Length >= ((this.sl[1] - this.sl[0]) + 1)) ? ((this.sl[1] - this.sl[0]) + 1) : (this.sl[1] - this.sl[0]);
                        this.Text = this.Text.Remove(((this.sl[0] - 1) >= 0) ? (this.sl[0] - 1) : 0, count);
                        this.watch -= count;
                    }
                }
                else if ((value == "\b") || (value == "BACK"))
                {
                    if ((this.Text.Length > 0) && (this.watch > 0))
                    {
                        this.Text = this.Text.Remove(this.watch - 1, 1);
                        this.watch--;
                    }
                }
                else if (((value == "DELETE") && (this.Text.Length > 0)) && (this.watch < this.Text.Length))
                {
                    this.Text = this.Text.Remove(this.watch, 1);
                }
            }
            else if ((value == "LEFT") || (value == "UP"))
            {
                if ((this.Text.Length > 0) && (this.watch > 0))
                {
                    this.watch--;
                }
            }
            else if ((value == "RIGHT") || (value == "DOWN"))
            {
                if (this.Text.Length > this.watch)
                {
                    this.watch++;
                }
            }
            else if (value == "CLEFT")
            {
                if ((this.Text.Length > 0) && (this.watch > 0))
                {
                    this.watch--;
                }
                while (this.watch > 0)
                {
                    if (!char.IsLetterOrDigit(this.Text[this.watch - 1]))
                    {
                        break;
                    }
                    this.watch--;
                }
            }
            else if (value == "CRIGHT")
            {
                if (this.Text.Length > this.watch)
                {
                    this.watch++;
                }
                while (this.watch < this.Text.Length)
                {
                    this.watch++;
                    if (!char.IsLetterOrDigit(this.Text[this.watch - 1]))
                    {
                        break;
                    }
                }
            }
            else
            {
                if (!(value == "SLEFT"))
                {
                    if (!(value == "SRIGHT"))
                    {
                        if (((this.sl[0] != -1) && (this.sl[1] != -1)) && (this.sl[0] != this.sl[1]))
                        {
                            int num2 = (this.Text.Length >= ((this.sl[1] - this.sl[0]) + 1)) ? ((this.sl[1] - this.sl[0]) + 1) : (this.sl[1] - this.sl[0]);
                            this.Text = this.Text.Remove(((this.sl[0] - 1) >= 0) ? (this.sl[0] - 1) : 0, num2);
                            this.watch -= num2;
                        }
                        this.Text = this.Text.Insert((this.watch < 0) ? 0 : this.watch, value);
                        this.watch += value.Length;
                        goto Label_05AC;
                    }
                    if (this.sel[0] == -1)
                    {
                        this.sel[0] = this.sel[1] = this.watch;
                    }
                    if (this.Text.Length > this.watch)
                    {
                        this.watch++;
                    }
                    while (this.watch < this.Text.Length)
                    {
                        this.watch++;
                        if (!char.IsLetterOrDigit(this.Text[this.watch - 1]))
                        {
                            break;
                        }
                    }
                }
                else
                {
                    if (this.sel[1] == -1)
                    {
                        this.sel[0] = this.sel[1] = this.watch;
                    }
                    if ((this.Text.Length > 0) && (this.watch > 0))
                    {
                        this.watch--;
                    }
                    while (this.watch > 0)
                    {
                        if (!char.IsLetterOrDigit(this.Text[this.watch - 1]))
                        {
                            break;
                        }
                        this.watch--;
                    }
                    if (this.sel[0] < this.watch)
                    {
                        this.sel[1] = this.watch;
                    }
                    else
                    {
                        this.sel[0] = this.watch;
                    }
                    goto Label_05AC;
                }
                if (this.sel[1] > this.watch)
                {
                    this.sel[0] = this.watch;
                }
                else
                {
                    this.sel[1] = this.watch;
                }
            }
        Label_05AC:
            this.tw = this.watch;
            if ((value != "SLEFT") && (value != "SRIGHT"))
            {
                int num5;
                int num6;
                this.sl[1] = num5 = -1;
                this.sl[0] = num6 = num5;
                this.sel[0] = this.sel[1] = num6;
            }
        }

        public int MousePlace(int x, int y)
        {
            string str = this.text.Replace("\r", " \r ");
            int watch = this.watch;
            this.g = Graphics.FromImage(new Bitmap(1, 1));
            Point point = new Point(0, 0);
            int height = (int) this.g.MeasureString("Х", base.Font).Height;
            int width = (int) this.g.MeasureString(" ", base.Font).Width;
            string[] strArray = str.Split(new char[] { ' ' });
            int num4 = 0;
            int num5 = 0;
            for (int i = 0; i < strArray.Length; i++)
            {
                if (strArray[i] == "\r")
                {
                    point.X = 0;
                    point.Y += height;
                    continue;
                }
                SizeF ef = this.g.MeasureString(strArray[i].ToString(), base.Font);
                if (((y >= point.Y) && (y <= (point.Y + height))) && ((x >= (point.X - ((width * 3) / 2))) && (x <= ((point.X + ef.Width) + (width / 2)))))
                {
                    bool flag = false;
                    for (int j = 0; j <= strArray[i].Length; j++)
                    {
                        int num8 = (j > 0) ? ((int) this.g.MeasureString(strArray[i].Substring(0, j).ToString(), base.Font).Width) : 0;
                        if ((x >= ((point.X + num8) - ((width * 3) / 2))) && (x <= ((point.X + num8) + (width / 2))))
                        {
                            watch = num4 + j;
                            flag = true;
                            break;
                        }
                    }
                    if (!flag)
                    {
                        watch = num4;
                    }
                    return watch;
                }
                if ((y >= point.Y) && (y <= (point.Y + height)))
                {
                    watch = num4 + strArray[i].Length;
                }
                point.X += (int) ef.Width;
                if (point.X > num5)
                {
                    num5 = point.X;
                }
                point.X += width;
                num4 += strArray[i].Length + 1;
            }
            return watch;
        }

        public override void OnMouseDown(MouseEventArgs e)
        {
            int num;
            int num2;
            bool flag = false;
            if ((this.sel[0] != -1) || (this.sel[1] != -1))
            {
                flag = true;
            }
            this.isclicked = true;
            this.sl[1] = num = -1;
            this.sl[0] = num2 = num;
            this.sel[0] = this.sel[1] = num2;
            this.X = e.X;
            this.Y = e.Y;
            this.tw = this.MousePlace(e.X, e.Y);
            if ((base.dblclick.Enabled && (this.watch >= 0)) && (this.tw == this.watch))
            {
                flag = true;
                base.dblclick.Enabled = false;
                this.sel = new int[] { this.tw, this.tw };
            }
            base.dblclick.Enabled = true;
            if (((this.watch != this.tw) || !base.Focused) || flag)
            {
                base.Focused = true;
                this.watch = this.tw;
                this.PrePainter();
                base.RePainter();
            }
        }

        public override void OnMouseMove(MouseEventArgs e)
        {
            if (this.isclicked && (((e.X < (this.X - 2)) || (e.X > (this.X + 2))) || ((e.Y < (this.Y - 2)) || (e.Y > (this.Y + 2)))))
            {
                this.watch = this.MousePlace(e.X, e.Y);
                if (this.watch >= this.tw)
                {
                    this.sel[0] = this.tw;
                    this.sel[1] = this.watch;
                }
                else
                {
                    this.sel[0] = this.watch;
                    this.sel[1] = this.tw;
                }
                if ((this.sl[0] != this.sel[0]) || (this.sl[1] != this.sel[1]))
                {
                    this.sl[0] = this.sel[0];
                    this.sl[1] = this.sel[1];
                    this.PrePainter();
                    base.RePainter();
                }
            }
        }

        public override void OnMouseUp(MouseEventArgs e)
        {
            this.isclicked = false;
            if ((((this.link != "") && (e.X >= 0)) && ((e.X < base.Right) && (e.Y > 0))) && ((e.Y < base.Bottom) && (e.Button == MouseButtons.Left)))
            {
                base.LinkClick();
            }
        }

        public override void OnPaint(PaintEventArgs e)
        {
            if (this.words == null)
            {
                this.PrePainter();
            }
            this.LC = 0;
            if (base.Focused || (base.BackColor.ToArgb() != Color.White.ToArgb()))
            {
                e.Graphics.FillRectangle(base.brush[1], (int) (e.ClipRectangle.X - 1), (int) (e.ClipRectangle.Y - 1), (int) (base.Size.Width - 6), (int) (base.Size.Height - 6));
            }
            for (int i = 0; i < this.words.Length; i++)
            {
                if (this.selected[i] && base.Focused)
                {
                    this.LC = this.a;
                    this.a = 1;
                    this.b = 2;
                    this.c = 3;
                    SizeF ef = e.Graphics.MeasureString(" ", base.Font);
                    SizeF ef2 = (this.words[i].Length > 0) ? e.Graphics.MeasureString(this.words[i], base.Font) : ef;
                    e.Graphics.FillRectangle(base.brush[this.b], e.ClipRectangle.X + this.wor[i].X, e.ClipRectangle.Y + this.wor[i].Y, ((int) ef2.Width) + ((int) ef.Width), (int) ef2.Height);
                }
                if (this.link != "")
                {
                    int width = (int) e.Graphics.MeasureString("_", base.Font).Width;
                    if ((this.wor[i].X - width) >= 0)
                    {
                        e.Graphics.DrawString("_", base.Font, base.brush[this.c], (float) ((e.ClipRectangle.X + this.wor[i].X) - width), (float) (e.ClipRectangle.Y + this.wor[i].Y));
                        e.Graphics.DrawString("_", base.Font, base.brush[this.c], (float) ((e.ClipRectangle.X + this.wor[i].X) - (width / 2)), (float) (e.ClipRectangle.Y + this.wor[i].Y));
                    }
                }
                e.Graphics.DrawString(this.words[i].ToString(), base.Font, base.brush[this.c], (float) (e.ClipRectangle.X + this.wor[i].X), (float) (e.ClipRectangle.Y + this.wor[i].Y));
                this.a = 0;
                this.b = 1;
                this.c = 0;
            }
            if ((this.watch >= 0) && base.Focused)
            {
                if ((this.sel[0] != -1) && (this.sel[1] != -1))
                {
                    this.a = 1;
                }
                e.Graphics.DrawLine(base.pen[this.LC], (int) (e.ClipRectangle.X + this.cur.X), (int) (e.ClipRectangle.Y + this.cur.Y), (int) (e.ClipRectangle.X + this.cur.X), (int) (((e.ClipRectangle.Y + this.cur.Y) + this.Vert) - 1));
            }
        }

        public override void PrePainter()
        {
            Graphics graphics = Graphics.FromImage(new Bitmap(1, 1));
            this.Vert = (int) graphics.MeasureString("Х", base.Font).Height;
            this.Hor = (int) graphics.MeasureString(" ", base.Font).Width;
            this.words = this.text.Replace("\r", " \r ").Split(new char[] { ' ' });
            this.wor = new Point[this.words.Length];
            this.selected = new bool[this.words.Length];
            int num = 0;
            int x = 0;
            bool flag = false;
            if (!this.bold)
            {
                if (this.link == "")
                {
                    base.Font = new Font("Courier New", (float) base.FontHeight, FontStyle.Regular);
                }
                else
                {
                    base.Font = new Font("Courier New", (float) base.FontHeight, FontStyle.Underline);
                }
            }
            else if (this.link == "")
            {
                base.Font = new Font("Courier New", (float) base.FontHeight, FontStyle.Bold);
            }
            else
            {
                base.Font = new Font("Courier New", (float) base.FontHeight, FontStyle.Underline | FontStyle.Bold);
            }
            base.brush = new SolidBrush[] { new SolidBrush((this.link != "") ? Color.DarkBlue : base.TextColor), new SolidBrush(base.BackColor), new SolidBrush(base.SelectedBackColor), new SolidBrush(base.SelectedTextColor) };
            Point point = new Point(0, 0);
            for (int i = 0; i < this.words.Length; i++)
            {
                if (this.words[i] == "\r")
                {
                    point.X = 0;
                    point.Y += this.Vert;
                    continue;
                }
                SizeF ef = graphics.MeasureString(this.words[i].ToString(), base.Font);
                this.wor[i] = new Point(point.X, point.Y);
                if (((num + this.words[i].Length) >= this.sel[0]) && (num < this.sel[1]))
                {
                    if (!flag)
                    {
                        this.sel[0] = this.sl[0] = num;
                        flag = true;
                    }
                    this.selected[i] = true;
                }
                else
                {
                    this.selected[i] = false;
                }
                for (int j = 0; j <= this.words[i].Length; j++)
                {
                    int num5 = (j > 0) ? ((int) graphics.MeasureString(this.words[i].Substring(0, j).ToString(), base.Font).Width) : 0;
                    if ((num + j) == this.watch)
                    {
                        this.cur = new Point((((point.X + num5) - 1) >= 0) ? ((point.X + num5) - 1) : 0, point.Y);
                        break;
                    }
                }
                point.X += (int) ef.Width;
                if (point.X > x)
                {
                    x = point.X;
                }
                point.X += this.Hor;
                num += this.words[i].Length + 1;
            }
            base.Size = new Size(((x == 0) ? 1 : x) + 8, (point.Y + this.Vert) + 8);
            base.PrePainter();
        }

        /*public bool RemoveText()
        {
            if (this.langText.Count > 1)
            {
                this.langText.Remove(this.currentLang);
                this.SetCurrentText();
                return true;
            }
            return false;
        }*/

        public void SetCurrentText()
        {
            /*string str;
            int num = -1;
            int num2 = -1;
            int num3 = -1;
            for (int i = LangSelection.Languages.GetLength(0) - 1; i >= 0; i--)
            {
                if (this.langText.ContainsKey(LangSelection.Languages[i, 0]))
                {
                    num3 = i;
                    if (LangSelection.Languages[i, 0] == "ENG")
                    {
                        num = i;
                    }
                    if (LangSelection.Languages[i, 0] == LangSelection.CurLang3Letter)
                    {
                        num2 = i;
                    }
                }
            }
            if (num2 != -1)
            {
                str = LangSelection.Languages[num2, 0];
            }
            else if (num != -1)
            {
                str = LangSelection.Languages[num, 0];
            }
            else if (num3 != -1)
            {
                str = LangSelection.Languages[num3, 0];
            }
            else
            {
                str = "ENG";
            }
            if (this.currentLang != str)
            {
                this.currentLang = str;
                this.text = this.langText[this.currentLang].ToString();
                this.PrePainter();
                base.RePainter();
            }*/
            this.Init();
        }

        /*public void SetText(string text, string lang)
        {
            this.currentLang = lang;
            this.Text = text;
        }*/

        /*public void ShowText(string lang)
        {
            this.currentLang = lang;
            if (this.langText.ContainsKey(lang))
            {
                this.text = this.langText[this.currentLang].ToString();
            }
            else
            {
                this.Text = string.Empty;
            }
            this.Init();
            this.PrePainter();
            base.RePainter();
        }*/

        public string StringModifier(string value)
        {
            return value.Replace("\r\n", "\r").Replace("\n", "\r");
        }

        protected override void UndoRedoManager()
        {
            //if (base.UnReDoPoint >= 0)
            //{
            //    if (((base.UnReDO[base.UnReDoPoint].Location != base.Location) || (base.UnReDO[base.UnReDoPoint].TextColor != base.TextColor)) || ((base.UnReDO[base.UnReDoPoint].BackColor != base.BackColor) || (base.UnReDO[base.UnReDoPoint].FontHeight != base.FontHeight)))
            //    {
            //        base.UnReDoPush(new UnReDO_Common());
            //        return;
            //    }
            //    if ((base.UnReDO[base.UnReDoPoint] is UnReDO_TextMemory) && (this.Text == ((UnReDO_TextMemory) base.UnReDO[base.UnReDoPoint]).value))
            //    {
            //        return;
            //    }
            //}
            //if (!string.IsNullOrEmpty(this.Text))
            //{
            //    base.UnReDoPush(new UnReDO_TextMemory(this.Text, this.sel, this.watch));
            //}
        }

        /*protected override void UnReDoAccept(UnReDO_Common unReDo_Common)
        {
            if (unReDo_Common is UnReDO_TextMemory)
            {
                UnReDO_TextMemory memory = (UnReDO_TextMemory) unReDo_Common;
                this.Text = memory.value;
                for (int i = 1; i >= 0; i--)
                {
                    this.sel[i] = memory.sel[i];
                }
                this.watch = memory.watch;
            }
            base.UnReDoAccept(unReDo_Common);
        }*/

        public override void UnReInit()
        {
            this.Text = "";
            this.watch = 0;
            this.sel = new int[] { -1, -1 };
            base.UnReInit();
        }

        public string Text
        {
            get
            {
                return this.text;
            }
            set
            {
                value = this.StringModifier(value);
                /*if (this.langText.ContainsKey(this.currentLang))
                {
                    this.langText[this.currentLang] = value;
                }
                else
                {
                    this.langText.Add(this.currentLang, value);
                }*/
                this.text = value;
            }
        }
    }
}

